<?php
class Fullhouse extends Vfriend_Db {
	var $cache;
	var $clear;
	var $notify;
	var $behaviour;
	var $mlogs;
	public function __construct() {
		$this->cache		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;
		$this->notify		= new Notification();
		$this->behaviour	= new Behaviour();
		$this->mlogs		= new Mlogs();
	}
	public function get($intUblastId) {
		try {
			$strKey 	= FULLHOUSE_GET . $intUblastId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ublasts_get(?)');
				$this->bindParam(1, $intUblastId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, FULLHOUSE_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::get - ' . $e->getMessage());
			return null;
		}
	}
	public function getAll($intUserId, $intOff, $intLimit,&$intTotal=0) {
		try {
			$this->prepare('call ublasts_getall(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::add - ' . $e->getMessage());
			return null;
		}
	}
	public function add($intPosterId, $intUserId, $strContent) {
		try {
			$this->prepare('call ublasts_add(?,?,?)');
			$this->bindParam(1, $intPosterId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContent, PDO::PARAM_STR);
			$arrResult = $this->fetchAll();
			$intError  = isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intUblastId = isset($arrResult[0]['ublastid']) ? $arrResult[0]['ublastid'] : 0;
				$this->clear->deleteUstat($intPosterId);				
				$strContentJson = Zend_Json::encode(array(	
															'posterid'	=> $intPosterId,
															'ownerid'	=> $intUserId,
															'ublastid'	=> $intUblastId,
															'cmt'		=> $strContent
														));
				$objVfriendUser = new Vfriend_User();
				if($intUserId == $intPosterId) {
					$arrRelation	= $objVfriendUser->getRelation($intUserId,2);
					$isHasOwner		= 0;
					if($arrRelation) {						
						foreach($arrRelation as $value) {	
							if($value == $intUserId) {
								$isHasOwner++;
							}							
							$this->mlogs->insert($value,50,$strContentJson);
							$this->notify->insert($value,49,$strContentJson,$intUblastId);
							$this->logBehaviour($value,$intUserId,$intPosterId,31,$intUblastId);
						}
					}
				}
				if(!$isHasOwner && $intUserId != $intPosterId) {
					$this->mlogs->insert($intUserId,50,$strContentJson);
					$this->notify->insert($intUserId,49,$strContentJson,$intUblastId);
					$this->logBehaviour($intUserId,$intUserId,$intPosterId,31,$intUblastId);
				}			
				$this->logBehaviour($intPosterId,$intUserId,$intPosterId,31,$intUblastId);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::add - ' . $e->getMessage());
			return null;
		}
	}
	public function delete($intUserId, $intUblastId) {
		try {
			$this->prepare('call ublasts_delete(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUblastId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->notify->deleteByType(49,$intUblastId);
				$this->notify->deleteByType(51,$intUblastId);
				$this->behaviour->deleteBySrcId(3,$intUblastId);
				$this->clear->deleteUblastById($intUblastId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::delete - ' . $e->getMessage());
			return null;
		}
	}
	public function getLike($intUblastId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {
			$strKey	 	= FULLHOUSE_GETLIKE . $intUblastId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ub_likes_get_top(?,?)');
				$this->bindParam(1, $intUblastId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, FULLHOUSE_GETLIKE_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::getLike - ' . $e->getMessage());
			return null;
		}
	}
	public function addLike($intUserId, $intUblastId,&$intTlike=0) {
		try {
			$this->prepare('call ub_likes_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUblastId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$strTitle	= isset($arrResult[0]['content']) ? $arrResult[0]['content'] : '';
				$intTlike 	= isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				$strContent = Zend_Json::encode(array('posterid'=>$intUserId,'ownerid'=>$intOwnerId,'ublastid'=>$intUblastId,'cmt'=>$strTitle));
				$this->notify->insert($intOwnerId,51,$strContent,$intUblastId);
				$this->clear->deleteUstat($intOwnerId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUblastById($intUblastId);
				// behaviour				
				/* $arrUser	= $this->getCmtUser($intUblastId);
				$isHasUser	= 0;
				if($arrUser) {
					$isHasOwner = 0;					
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];											
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 33, $intUblastId);						
					}
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 33, $intUblastId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 33, $intUblastId); */
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::addLike - ' . $e->getMessage());
			return null;
		}
	}
	public function getCmtTotal($intUblastId) {
		try {
			$strKey 	= FULLHOUSE_GETCMTTOTAL . $intUblastId;
			$arrResult	= $this->cache->get($intUblastId);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ub_comments_gettotal(?)');
				$this->bindParam(1, $intUblastId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, FULLHOUSE_GETCMTTOTAL_EXPIRE);
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $intTotal;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::getCmtTotal - ' . $e->getMessage());
			return null;
		}
	}
	public function getCmtAll($intType, $intUblastId, $intOff, $intLimit) {
		try {
			$this->prepare('call ub_comments_getall(?,?,?,?)');
			$this->bindParam(1, $intType, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUblastId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::getCmtAll - ' . $e->getMessage());
			return null;
		}
	}
	public function getTopCmt($intUblastId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		$strKey 	= FULLHOUSE_GETTOPCMT . $intUblastId;
		$arrResult	= $this->cache->get($strKey);		
		if(!Vfriend_Function::isCached($arrResult)) {
			$arrResult	= $this->getCmtAll(1,$intUblastId,0,$intLimit);
			$this->cache->set($strKey, $arrResult, FULLHOUSE_GETTOPCMT_EXPIRE);
		}
		return $arrResult;
	}
	public function getCmtUser($intUblastId) {
		try {	
			$strKey 	= FULLHOUSE_GETCMTUSER . $intUblastId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ub_comments_get_user(?)');
				$this->bindParam(1, $intUblastId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();			
				$this->cache->set($strKey, $arrResult, FULLHOUSE_GETCMTUSER_EXPIRE);
			}
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::getCmtUser - ' .$e->getMessage());			
			return null;
		}
	}
	public function addCmt($intUserId, $intUblastId, $strContent,&$intOwnerId=0) {
		try {
			$this->prepare('call ub_comments_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUblastId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContent, PDO::PARAM_STR);
			$arrResult	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
			if($intError == 0) {
				$strTitle	= isset($arrResult[0]['content']) ? $arrResult[0]['content'] : '';
				$intCmtId	= isset($arrResult[0]['ubcommentid']) ? $arrResult[0]['ubcommentid'] : 0;				
				$this->clear->deleteUstat($intOwnerId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUblastById($intUblastId);
				$arrUser	= $this->getCmtUser($intUblastId);				
				if($arrUser) {
					$isHasOwner = 0;
					$strContentJson = Zend_Json::encode(
														array(	'posterid'=>$intUserId,
																'ownerid'=>$intOwnerId,
																'ublastid'=>$intUblastId,
																'title'=>$strTitle,
																'cmt'=>$strContent
															)
													);
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];
						if($intReceiveId == $intOwnerId) {
							$isHasOwner++;
						}
						if($intUserId != $intReceiveId) {
							$this->mlogs->insert($intReceiveId,51,$strContentJson);
							$this->notify->insert($intReceiveId,50,$strContentJson,$intCmtId);
						}
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 32, $intUblastId);	
					}
					if(!$isHasOwner && $intUserId != $intOwnerId) {
						$this->mlogs->insert($intOwnerId,51,$strContentJson);
						$this->notify->insert($intOwnerId,50,$strContentJson,$intCmtId);
					}
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 32, $intUblastId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 32, $intUblastId);
			} 
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::addCmt - ' . $e->getMessage());
			return null;
		}
	}
	public function deleteCmt($intUserId, $intUblastId, $intUbcommentId) {
		try {
			$this->prepare('call ub_comments_delete(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUblastId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intUbcommentId, PDO::PARAM_INT, 11);
			if($this->execute()) {
				$this->clear->deleteUblastById($intUblastId);
				$this->notify->deleteByType(50,$intUbcommentId);
				$this->notify->deleteByType(52,$intUbcommentId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::deleteCmt - ' . $e->getMessage());
			return null;
		}
	}
	public function likeCmt($intUserId, $intUblastId, $intUbcommentId, &$intTlike=0) {
		try {
			$this->prepare('call ubc_likes_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUblastId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intUbcommentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strTitle 	= isset($arrResult[0]['title']) ? $arrResult[0]['title'] : '';
				$strContent = isset($arrResult[0]['content']) ? $arrResult[0]['content'] : '';
				$intOwnerId = isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : '';
				$intCmtorId = isset($arrResult[0]['cmtorid']) ? $arrResult[0]['cmtorid'] : '';
				$intTlike 	= isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				// clear cache
				$this->clear->deleteUstat($intOwnerId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUblastById($intUblastId);
				// notify
				$strContentJson = Zend_Json::encode(
														array(	'posterid'=>$intUserId,
																'ownerid'=>$intOwnerId,
																'ublastid'=>$intUblastId,
																'title'=>$strTitle,
																'cmt'=>$strContent
															)
													);
				$this->notify->insert($intCmtorId,52,$strContentJson,$intUbcommentId);
				// behaviour				
				/* $arrUser	= $this->getCmtUser($intUblastId);
				$isHasUser	= 0;
				if($arrUser) {
					$isHasOwner = 0;					
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];											
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 34, $intUblastId);						
					}
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 34, $intUblastId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 34, $intUblastId); */
				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::likeCmt - ' . $e->getMessage());
			return null;
		}
	}
	public function getQuote($intUblastId, $intType) {
		try {
			$strKey	 	= FULLHOUSE_GETQUOTE . $intUblastId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ublasts_get_quote(?,?)');
				$this->bindParam(1, $intUblastId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, FULLHOUSE_GETQUOTE_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::getQuote - ' . $e->getMessage());
			return null;
		}
	}
	public function getRelation($intUserId, $intViewId, $strFriend, $intType, $intSort, $intOff, $intLimit, &$intTotal=0) {
		try {
			$this->prepare('call urelations_getall(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intViewId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strFriend, PDO::PARAM_STR);
			$this->bindParam(4, $intType, PDO::PARAM_INT, 4);
			$this->bindParam(5, $intSort, PDO::PARAM_INT, 4);
			$this->bindParam(6, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;			
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Fullhouse::getRelation - ' . $e->getMessage());
			return null;
		}
	}
	public function logBehaviour($intUserId,$intOwnerId,$intPosterId,$intType,$intSrcId) {
		$strQueryCore 		= 'get';
		$strQueryLike		= 'getLike';
		$strQueryTotalCmt	= 'getCmtTotal';
		$strQueryCmt		= 'getTopCmt';		
		$strQueryQuote		= 'getQuote';
		$this->behaviour->insert($intUserId,
								 $intOwnerId,
								 $intPosterId,
								 $strQueryCore,
								 $strQueryLike,
								 $strQueryTotalCmt,
								 $strQueryCmt,
								 $strQueryQuote,
								 $intType,
								 $intSrcId);
	}
}
?>